<template>
	<view class="content">
		<view class="custom-back-icon" style="display: flex;height: 88upx;line-height: 88upx;"
			:style="{'margin-top': (systemInfo.backPosition-7)+'px'}">
			<uni-icons type="left" size="30" @click="goBack"></uni-icons>
		</view>
		<view class="search-box">
			<input type="text" placeholder="请输入搜索关键词"  v-model="searchTxt" />
			<text @click="searchByGoodsName">搜索</text>
		</view>
		
		<view class="hot-search-text" v-if="!searchTxt">
			<view class="title-box">
				<image src="https://qiniu.dadaozhixing.top/dxx/pages/product/hot_search.png"></image>
				<text style="font-weight: 600;font-size: 32upx;">热门搜索</text>
			</view>
			<view class="search-list">
				<span v-for="txt in hotSearchTxtList" @click="searchCurGoodsName(txt)">{{txt}}</span>
			</view>
		</view>

		<view class="goods-list">
			<view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
				<view class="image-wrapper">
					<image :src="item.icon" mode="aspectFill"></image>
				</view>
				<view class="text-box">
					<text class="title clamp">{{item.name}}</text>
					<text class="sub-title clamp">{{item.name}}</text>
					<view class="price-box">
						<view v-if="hasLogin">
							<text class="price">{{item.unitPrice}}</text>
							<image class="add-to-cart"
								src="https://qiniu.dadaozhixing.top/dxx/pages/product/add_2_cart.png"
								@click.stop="add2Cart(item.id)"></image>
						</view>
						<view v-else>
							<text class="price">登录查看价格</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cate-mask" :class="cateMaskState===0 ? 'none' : cateMaskState===1 ? 'show' : ''"
			@click="toggleCateMask">
			<view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
				<scroll-view scroll-y class="cate-list">
					<view v-for="item in cateList" :key="item.id">
						<view class="cate-item b-b two">{{item.name}}</view>
						<view v-for="tItem in item.child" :key="tItem.id" class="cate-item b-b"
							:class="{active: tItem.id==cateId}" @click="changeCate(tItem)">
							{{tItem.name}}
						</view>
					</view>
				</scroll-view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	import {
		typeListApi,
		goodsListApi,
		goodsPageListApi,
		goodsSizeListApi,
		add2CartApi
	}
	from "@/api/goods";
	import { sysConfigValueApi } from '@/api/sysConfig'
	export default {
		data() {
			return {
				searchTxt: '',
				hotSearchTxtList: ['鸡锁骨','鸡爪','牛肉','羊肉','羊排'],
				cateMaskState: 0, //分类面板展开状态
				headerPosition: "fixed",
				headerTop: "0px",
				loadingType: 'more', //加载更多状态
				filterIndex: 0,
				cateId: 0, //已选三级分类id
				priceOrder: 0, //1 价格从低到高 2价格从高到低
				cateList: [],
				goodsList: [],
				goodsSizeList: []
			};
		},

		onLoad(options) {
			this.cateId = options.fid;
			this.loadCateList(options.fid);
			this.sysConfigList()
			this.loadData();
		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			if (e.scrollTop >= 0) {
				this.headerPosition = "fixed";
			} else {
				this.headerPosition = "absolute";
			}
		},
		//下拉刷新
		onPullDownRefresh() {
			this.loadData('refresh');
		},
		//加载更多
		onReachBottom() {
			this.loadData();
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'systemInfo'])
		},
		methods: {
			...mapActions([
				"goBack"
			]),
			async sysConfigList () {
				let ret = await sysConfigValueApi({"type": "goods_hot_search_list", "name": "goods_name"})
				console.log(ret);
				if (ret && ret.code == 200) {
					this.hotSearchTxtList = JSON.parse(ret.msg)
				}
			},
			//加载分类
			async loadCateList(goodsId) {
				if (goodsId) {
					let ret = await goodsPageListApi({
						id: goodsId
					})
					this.goodsList = ret.rows
				}else {
					this.searchByGoodsName()
				}
			},
			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type = 'add', loading) {
				//没有更多直接返回
				if (type === 'add') {
					if (this.loadingType === 'nomore') {
						return;
					}
					this.loadingType = 'loading';
				} else {
					this.loadingType = 'more'
				}


			},
			//筛选点击
			tabClick(index) {
				if (this.filterIndex === index && index !== 2) {
					return;
				}
				this.filterIndex = index;
				if (index === 2) {
					this.priceOrder = this.priceOrder === 1 ? 2 : 1;
				} else {
					this.priceOrder = 0;
				}
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
			},
			//显示分类面板
			toggleCateMask(type) {
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.cateMaskState = 2;
				setTimeout(() => {
					this.cateMaskState = state;
				}, timer)
			},
			//分类点击
			changeCate(item) {
				this.cateId = item.id;
				this.toggleCateMask();
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
			},
			//详情
			navToDetailPage(item) {
				//测试数据没有写id，用title代替
				let id = item.id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
			stopPrevent() {},
			async add2Cart(goodsSizeId) {
				// let ret = await add2CartApi({
				// 	customerId: this.userInfo.id,
				// 	goodsSizeId
				// })
				this.$api.msg('添加到购物车');
				// uni.navigateTo({
				// 	url: ''
				// })
			},
			async searchByGoodsName() {
				console.log(this.searchTxt);
				let ret = await goodsPageListApi({
					name: this.searchTxt,
					level: 1,
					merId: 1,
					shopId: 1
				})
				this.goodsList = ret.rows
			},
			searchCurGoodsName(txt) {
				this.searchTxt = txt
				this.searchByGoodsName()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-box {
		height: 60upx;
		border-radius: 32upx;
		padding: 0;
		display: flex;
		align-items: center;

		input {
			height: 60upx;
			font-size: 28upx;
			width: 566upx;
			border: 1rpx solid #3EB3F3 !important;
			border-radius: 32rpx;
			padding-left: 24upx;
			margin-right: 24upx;
		}
		
		text {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #23242E;
			line-height: 42rpx;
			text-align: left;
			font-style: normal;
		}
	}

	page,
	.content {
		background: $page-color-base;
		padding: 24upx;
	}

	.content {
		padding: 24upx;
	}

	.navbar {
		position: fixed;
		left: 0;
		top: var(--window-top);
		display: flex;
		width: 100%;
		height: 80upx;
		background: #fff;
		box-shadow: 0 2upx 10upx rgba(0, 0, 0, .06);
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30upx;
			color: $font-color-dark;
			position: relative;

			&.current {
				color: $base-color;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 120upx;
					height: 0;
					border-bottom: 4upx solid $base-color;
				}
			}
		}

		.p-box {
			display: flex;
			flex-direction: column;

			.yticon {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 14upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #888;

				&.active {
					color: $base-color;
				}
			}

			.xia {
				transform: scaleY(-1);
			}
		}

		.cate-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 44upx;

			&:after {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}

	/* 分类 */
	.cate-mask {
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 95;
		transition: .3s;

		.cate-content {
			width: 630upx;
			height: 100%;
			background: #fff;
			float: right;
			transform: translateX(100%);
			transition: .3s;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.cate-content {
				transform: translateX(0);
			}
		}
	}

	.cate-list {
		display: flex;
		flex-direction: column;
		height: 100%;

		.cate-item {
			display: flex;
			align-items: center;
			height: 90upx;
			padding-left: 30upx;
			font-size: 28upx;
			color: #555;
			position: relative;
		}

		.two {
			height: 64upx;
			color: #303133;
			font-size: 30upx;
			background: #f8f8f8;
		}

		.active {
			color: $base-color;
		}
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-wrap: wrap;

		.goods-item {
			display: flex;
			background: #fff;
			align-items: center;
			height: 228upx;
			width: 100%;
			border-radius: 16upx;
		}

		.image-wrapper {
			width: 170upx;
			height: 170upx;
			margin: 29upx 29upx 21upx 29upx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.text-box {
			display: flex;
			flex-direction: column;

			.sub-title {
				font-size: 24rpx;
				color: #7F808A;
				line-height: 36rpx;
			}
		}

		.title {
			font-weight: 600;
			font-size: 28rpx;
			color: #23242E;
			line-height: 42rpx;
		}

		.price-box {
			margin-top: 52upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $font-color-light;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 26upx;
			}
		}

		.add-to-cart {
			margin-left: 250upx;
			width: 36upx;
			height: 36upx;
		}
	}
	.hot-search-text{
		margin-top: 56upx;
		.title-box {
			height: 48upx;
			line-height: 48upx;
			display: flex;
			align-items: center;
			image {
				width: 40upx;
				height: 40upx;
			}
		}
		.search-list {
			display: flex;
			flex-wrap: wrap;
			span {
				padding: 0 24upx;
				margin-right: 24upx;
				margin-top: 24upx;
				height: 56upx;
				line-height: 56upx;
				background: #F1F1F1;
				border-radius: 28rpx;
				font-size: 24upx;
			}
		}
		
	}
</style>